package com.eatme.leetcode.problems;

import java.util.TreeMap;

/**
 * 208. 实现 Trie (前缀树) - implement-trie-prefix-tree
 */
public class Trie208 {

    private class Node {
        public boolean isWord;
        private TreeMap<Character, Node> next;

        public Node(boolean isWord) {
            this.isWord = isWord;
            next = new TreeMap<>();
        }

        public Node() {
            this(false);
        }
    }

    private Node root;
    private int size;

    public Trie208() {
        root = new Node();
        size = 0;
    }

    // 获得Trie中存储的单词数量
    public int getSize() {
        return size;
    }

    // 向Trie中添加一个新的单词word
    public void insert(String word) {
        Node cur = root;
        for (int i = 0; i < word.length(); i++) {
            char c = word.charAt(i);
            if (cur.next.get(c)==null)
                cur.next.put(c,new Node());
            cur = cur.next.get(c);
        }
        if (!cur.isWord) {
            cur.isWord =true;
            size++;
        }
    }

    // 查看单词word是否在Trie中
    public boolean search(String word) {
        Node cur = root;
        for (int i = 0; i < word.length(); i++) {
            char c =word.charAt(i);
            if (cur.next.get(c)==null)
                return false;
            cur = cur.next.get(c);
        }
        return cur.isWord;
    }

    // 查询是否在Trie 中有单词以prefix为前缀
    public boolean startsWith(String prefix) {
        Node cur = root;
        for (int i = 0; i < prefix.length(); i++) {
            char c =prefix.charAt(i);
            if (cur.next.get(c)==null)
                return false;
            cur = cur.next.get(c);
        }
        return true;
    }

    public static void main(String[] args) {
        new Trie208().insert("cat");
        System.out.println(new Trie208().search("cat"));
    }




}
